/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val)
{
    ListNode* pcur = head, * prev = NULL;
    while (pcur)
    {
        ListNode* next = pcur->next;
        if (pcur->val == val)
        {
            ListNode* del = pcur;
            if (pcur == head)
                head = pcur->next;
            else
                prev->next = pcur->next;
            free(pcur);
        }
        else
            prev = pcur;
        pcur = next;
    }
    return head;
}